package com.pagepub.flex.factory;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import flex.messaging.FactoryInstance;
import flex.messaging.FlexContext;
import flex.messaging.config.ConfigMap;
import flex.messaging.services.ServiceException;

public class SpringFactoryInstance extends FactoryInstance {
	public SpringFactoryInstance(FlexSpringFactory factory, String id,
			ConfigMap properties) {
		super(factory, id, properties);
	}

	public Object lookup() {
		ApplicationContext appContext = WebApplicationContextUtils
				.getWebApplicationContext(FlexContext.getServletConfig()
						.getServletContext());
		String beanName = this.getSource();
		try {
			return appContext.getBean(beanName);
		} catch (NoSuchBeanDefinitionException ec) {
			ServiceException e = new ServiceException();
			throw e;
		} catch (BeansException bexc) {
			ServiceException e = new ServiceException();
			throw e;
		}
	}
}
